package com.guashu.jaywayjsonpath.renhang;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.Option;
import net.minidev.json.JSONArray;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JsonPathProcessor {
    public static void main(String[] args) throws IOException {
        String jsonInput = "{...}"; // 将这里的"..."替换为提供的JSON字符串
        ObjectMapper mapper = new ObjectMapper();
        Object document = Configuration.defaultConfiguration()
                .jsonProvider()
                .parse(jsonInput);

        // 解析规则
        Map<String, String> fieldJsonPaths = new HashMap<>();
        fieldJsonPaths.put("pa01bi01", "$.PRH.PA01.PA01B.PA01BI01");
        fieldJsonPaths.put("pd01ad01", "$.PDA.PD01[*].PD01A.PD01AD01");
        fieldJsonPaths.put("pd01bd01", "$.PDA.PD01[*].PD01B.PD01BD01");
        fieldJsonPaths.put("pd01ai01", "$.PDA.PD01[*].PD01A.PD01AI01");
        fieldJsonPaths.put("pd01er03", "$.PDA.PD01[*].PD01E.PD01EH[*].PD01ER03");
        fieldJsonPaths.put("pd01ed01", "$.PDA.PD01[*].PD01E.PD01EH[*].PD01ED01");
        fieldJsonPaths.put("pd01ej01", "$.PDA.PD01[*].PD01E.PD01EH[*].PD01EJ01");

        List<String> strList = new ArrayList<>();
        String pa01bi01 = JsonPath.read(document, fieldJsonPaths.get("pa01bi01"));
        List<String> pd01ad01List = JsonPath.read(document, fieldJsonPaths.get("pd01ad01"));
        List<String> pd01bd01List = JsonPath.read(document, fieldJsonPaths.get("pd01bd01"));
        List<String> pd01ai01List = JsonPath.read(document, fieldJsonPaths.get("pd01ai01"));

        for (int i = 0; i < pd01ad01List.size(); i++) {
            JSONArray pd01er03Array = JsonPath.read(document, fieldJsonPaths.get("pd01er03").replace("[*]", "[" + i + "]"));
            JSONArray pd01ed01Array = JsonPath.read(document, fieldJsonPaths.get("pd01ed01").replace("[*]", "[" + i + "]"));
            JSONArray pd01ej01Array = JsonPath.read(document, fieldJsonPaths.get("pd01ej01").replace("[*]", "[" + i + "]"));

            for (int j = 0; j < pd01er03Array.size(); j++) {
                String pd01er03 = pd01er03Array.get(j).toString();
                String pd01ed01 = pd01ed01Array.get(j).toString();
                String pd01ej01 = pd01ej01Array.get(j).toString();

                String result = String.join(",", pa01bi01, pd01ad01List.get(i), pd01bd01List.get(i), pd01ai01List.get(i), pd01er03, pd01ed01, pd01ej01);
                strList.add(result);
            }
        }

        // 打印结果
        for (String s : strList) {
            System.out.println(s);
        }
    }
}
